﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VNWIKI_WEB;
using VNWIKI_WEB.Models;

namespace VNWIKI_WEB.Controllers
{ 
    public class PhotoController : Controller
    {
        VNWIKIDataContext db = new VNWIKIDataContext();

        //
        // GET: /Phto/

        public ActionResult Index()
        {
            User admin = (User)Session["user"];
            if (admin == null || admin.Admin == false)
                return RedirectToAction("LogOn", "Admin");

            var photos = db.Photos.Include(p => p.Album);
            return View(photos.ToList());
        }

        //
        // GET: /Phto/Details/5

        public ActionResult Details(int id)
        {
            User admin = (User)Session["user"];
            if (admin == null || admin.Admin == false)
                return RedirectToAction("LogOn", "Admin");

            Photo photo = db.Photos.Single(u => u.ID == id);
            return View(photo);
        }

        //
        // GET: /Phto/Create

        public ActionResult Create()
        {
            User admin = (User)Session["user"];
            if (admin == null || admin.Admin == false)
                return RedirectToAction("LogOn", "Admin");

            ViewBag.AlbumID = new SelectList(db.Albums, "ID", "Name");
            return View();
        } 

        //
        // POST: /Phto/Create

        [HttpPost]
        public ActionResult Create(Photo photo)
        {
            if (ModelState.IsValid)
            {
                db.Photos.InsertOnSubmit(photo);
                db.SubmitChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.AlbumID = new SelectList(db.Albums, "ID", "Name", photo.AlbumID);
            return View(photo);
        }
        
        //
        // GET: /Phto/Edit/5
 
        public ActionResult Edit(int id)
        {
            User admin = (User)Session["user"];
            if (admin == null || admin.Admin == false)
                return RedirectToAction("LogOn", "Admin");

            Photo photo = db.Photos.Single(u => u.ID == id);
            ViewBag.AlbumID = new SelectList(db.Albums, "ID", "Name", photo.AlbumID);
            return View(photo);
        }

        //
        // POST: /Phto/Edit/5

        [HttpPost]
        public ActionResult Edit(Photo photo)
        {
            if (ModelState.IsValid)
            {
                Photo p = db.Photos.Single(u => u.ID == photo.ID);
                p.Description = photo.Description;
                p.AlbumID = photo.AlbumID;
                p.CreateDate = p.CreateDate;
                p.Deleted = p.Deleted;
                p.DeleteDate = p.DeleteDate;
                p.Link = photo.Link;
                p.Name = photo.Name;
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            ViewBag.AlbumID = new SelectList(db.Albums, "ID", "Name", photo.AlbumID);
            return View(photo);
        }

        //
        // GET: /Phto/Delete/5
 
        public ActionResult Delete(int id)
        {
            User admin = (User)Session["user"];
            if (admin == null || admin.Admin == false)
                return RedirectToAction("LogOn", "Admin");

            Photo photo = db.Photos.Single(u => u.ID == id);
            return View(photo);
        }

        //
        // POST: /Phto/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Photo photo = db.Photos.Single(u => u.ID == id);
            db.Photos.DeleteOnSubmit(photo);
            db.SubmitChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}